package com.wallace.downloadserver.util.myThread

import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.scheduling.annotation.EnableAsync
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor
import java.util.concurrent.Executor

/**
 * <Description>
 * MyThreadPool
 * @author Wallace
 * @Date 2021/11/19 10:35
 */
@Configuration
@EnableAsync
class MyThreadPool {

    private val log: Logger = LoggerFactory.getLogger(MyThreadPool::class.java)

    @Bean(name = ["WallacePool"])
    fun createThreadPool(): Executor {
        val taskExecutor = ThreadPoolTaskExecutor()
        taskExecutor.corePoolSize = ThreadPoolConfig.CORE_POOL_SIZE
        taskExecutor.maxPoolSize = ThreadPoolConfig.IO_MAXIMUM_POOL_SIZE
        taskExecutor.setQueueCapacity(100)
        taskExecutor.setThreadFactory(ThreadPoolConfig.threadFactory)
        taskExecutor.setRejectedExecutionHandler { r, executor ->
            log.warn(("task $r discard by $executor"))
        }
        return taskExecutor
    }
}